<?php
class GuestbookAction extends CommonAction {	
	private $pageUrl = '/guestbook/';
	
	public function __construct(){
		parent::__construct();
		
		$this->modelObject = D('Guestbook');
    }
	 public function index(){
		$map = array();
		$map['status'] = 1;
		
		if($thisPage = I('p', 0)){
			$thisPage = substr($thisPage,1);	
		}else{
			$thisPage = 1;
		}
		
				
		//获得列表
		$list = $this->modelObject->getPageList($map, $thisPage);
		$this->assign('list', $list);
		$this->assign('pageUrl', $this->pageUrl.'p');
		
		//分页
		$page = $this->modelObject->buildPage($map, $thisPage);
		$this->assign('page',$page);
		
		$this->setSeo(array('title'=>'在线留言'));
		$this->display();
	}
	
	public function save(){
		if(IS_POST){
			$_POST['member_id'] = $this->memberInfo['id'];
			$_POST['add_time']  = NOW_TIME;
			$_POST['ip']        = get_client_ip(1);
			
			$name       = I('name', '');
			$tel        = I('tel', '');
			$title      = I('title', '');
			$content    = I('content', '');
			if(
				empty($name)
				|| empty($tel)
				|| empty($title)
				|| empty($content)
			){
				$this->error('所有字段都必须填写!');		
			}
			
			if(!isTel($tel) && !isEmail($tel)){
				$this->error('联系方式请写手机号码,或正确的邮箱地址!');			
			}
			
			$guestbookLockInfo = $this->webConfig['guestbook_lock'];
			$guestbookLockArr = explode('|', trim($guestbookLockInfo));

			if(!empty($guestbookLockArr[0]) && (in_array($_POST['ip'], $guestbookLockArr))){
				$this->error('你的操作异常,已被系统限制留言,如有疑问,请联系 '.$this->webConfig['email'], U('/guestbook/'));	
			}elseif($this->webConfig['post_lock_website'] == 1 && hasWebsite($_POST['content'])){//是否屏蔽网址
				$this->error('留言中不能有网址,感谢您的支持!');		
			}else{
				//判断ip,限制600秒只能提交一次
				$lastPostTime = $this->modelObject->where('ip = %d OR tel = %s', $_POST['ip'], $_POST['tel'])->order('id DESC')->limit(1)->getField('add_time');
				if(NOW_TIME - $lastPostTime > 600){
					if($this->modelObject->create()){
						if($this->modelObject->add()){
							$this->success('留言成功,请等待管理员审核!', U('/guestbook'));
						}else{
							$this->error('留言失败!');
						}
					}else{
						$this->error($this->modelObject->getError());
					}
				}else{
					$this->error('你刚留言过,请休息10分钟!');
				}
			}
		}
	}
}